//
//  AnimationHelper.m
//  AnimationTest
//
//  Created by minjunxu on 12-4-27.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AnimationHelper.h"

@implementation AnimationHelper

+ (CAAnimation*)animationWithKeyPath:(NSString*)keyPath 
                            duration:(CFTimeInterval)duration
                                form:(id)fromValue
                                  to:(id)toValue
                            delegate:(id)delegate
{
    __autoreleasing CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:keyPath];  
    animation.duration = duration;
    animation.fromValue = fromValue;
    animation.toValue = toValue;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:@"default"];
    animation.delegate = delegate;

    return animation;
}


+ (void)addAnimationForView:(UIImageView *)view 
       animationWithKeyPath:(NSString*)keyPath 
                   duration:(CFTimeInterval)duration
                       form:(id)fromValue
                         to:(id)toValue
                   delegate:(id)delegate
                        key:(NSString*)key      
{
    [view.layer addAnimation:[self animationWithKeyPath:keyPath duration:duration form:fromValue to:toValue delegate:delegate]
                      forKey:key];
}

+ (void)simpleAnimationForView:(UIImageView *)view 
          animationWithKeyPath:(NSString*)keyPath 
                      duration:(CFTimeInterval)duration
                          form:(id)fromValue
                            to:(id)toValue
{
    [AnimationHelper addAnimationForView:view
                    animationWithKeyPath:keyPath
                                duration:duration
                                    form:fromValue 
                                      to:toValue
                                delegate:nil
                                     key:nil];
}


+ (void)addAnimationForView:(UIImageView*)view 
                        withOptions:(NSInteger)options
                           duration:(CFTimeInterval)duration
                           delegate:(id)delegate
                                key:(NSString*)key
{
    if (options & ANIMATION_OPACITY_MASK)
    {
        [view.layer addAnimation: [AnimationHelper animationWithKeyPath:@"opacity" 
                                                               duration:duration
                                                                   form:[NSNumber numberWithFloat: 0.0]
                                                                     to:[NSNumber numberWithFloat: 1.0]
                                                               delegate:delegate]
                          forKey: key];
    }
    
    if (options & ANIMATION_SCALE_MASK)
    {
        [view.layer addAnimation: [AnimationHelper animationWithKeyPath:@"transform.scale" 
                                                               duration:duration
                                                                   form:[NSNumber numberWithFloat: 0.0]
                                                                     to:[NSNumber numberWithFloat: 1.0]
                                                               delegate:delegate]
                          forKey: key];
    }
    
    if (options & ANIMATION_ROTATE_MASK_CLOCK)
    {
        [view.layer addAnimation: [AnimationHelper animationWithKeyPath:@"transform.rotation.z" 
                                                               duration:duration
                                                                   form:[NSNumber numberWithFloat: 0.0]
                                                                     to:[NSNumber numberWithFloat: M_PI*2.0]
                                                                delegate:delegate]
                          forKey: key];
    }
    
    if (options & ANIMATION_ROTATE_MASK_COUNTER)
    {
        [view.layer addAnimation: [AnimationHelper animationWithKeyPath:@"transform.rotation.z" 
                                                               duration:duration
                                                                   form:[NSNumber numberWithFloat: 0.0]
                                                                     to:[NSNumber numberWithFloat: M_PI*-2.0]
                                                               delegate:delegate]
                          forKey: key];
    }
}

+ (void)simpleAnimationForView:(UIImageView*)view 
                   withOptions:(NSInteger)options
                      duration:(CFTimeInterval)duration
{
    [AnimationHelper addAnimationForView:view
                             withOptions:options
                                duration:duration
                                delegate:nil
                                     key:nil];
}

@end
